package com.wj.jiangan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wj.jiangan.pojo.Product;
import com.wj.jiangan.pojo.vo.productStockSortVo;
import com.wj.jiangan.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wj
 * @since 2019-11-05
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;

    /**
     * 根据产品类别sid查询所有存在的产品
     * @param sid
     * @return
     */
    @GetMapping("/existp")
    public List<Product> getExistProductBySid(@RequestParam Integer sid){
        return productService.getExistProductBySid(sid);
    }


    @RequestMapping("/queryPartByPId")
    public Map queryPartByPId(Integer pId){
        System.out.println(pId);
        QueryWrapper<Product> partQueryWrapper = new QueryWrapper<>();
        partQueryWrapper.eq("p_id",pId);
        List<Product> products = productService.queryPoductByPId(partQueryWrapper);
        System.out.println(products);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("part",products);
        return map;
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @RequestMapping("/updateProductByPId")
    public Map updatePartByPPId(Product product){
        productService.updateProductByPId(product);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        return map;
    }


    /**
     * 获取产品的所有产地
     *
     * @param pname 产品名称
     * @return
     */
    @GetMapping("/productArea")
    public List<Map<String, Object>> getProductAreaByPName(@RequestParam String pname){
        return productService.getProductAreaByPName(pname);
    }

    /**
     * 返回所有的产品
     * @return
     */
    @GetMapping("productList")
    public List<Product> selectProduct() {
        return productService.selectAllProduect();
    }

    /**
     * 分页
     *
     * @param page
     * @return
     */
    @GetMapping("findProductPage")//类别分页
    public Map<String, Object> findpageProductNum(Page<Product> page) {
        IPage<Product> sproductIPage = productService.findProductByPage(page);
        List<Map<String, Object>> list = new ArrayList<>();
        sproductIPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("pid", o.getPId());
            map.put("pname", o.getPName());
            map.put("parea", o.getPArea());
            map.put("pml", o.getPMl());
            list.add(map);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "");
        result.put("count", sproductIPage.getTotal());
        result.put("data", list);
        return result;
    }

    //查询库存商品的信息(分页)
    @GetMapping("pss")
    public Map<String, Object> findAllpssvo(Page page, @RequestParam(required = false) Integer sid, @RequestParam(required = false) Integer pid) {
        Map<String, Object> param = new HashMap<>();
        param.put("sid", sid);
        param.put("pid", pid);
        IPage<productStockSortVo> pdssvo = productService.findpssvoPage(page, param);
        List<Map<String, Object>> list = new ArrayList<>();
        pdssvo.getRecords().forEach(o -> {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("pid", o.getProduct().getPId());
            map1.put("sname", o.getSort().getSName());
            map1.put("pname", o.getProduct().getPName());
            map1.put("parea", o.getProduct().getPArea());
            map1.put("pml", o.getProduct().getPMl());
            map1.put("ksum", o.getStock().getKSum());
            list.add(map1);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("count", pdssvo.getTotal());
        result.put("data", list);
        return result;
    }
        //添加
    @PostMapping("saveProduct")
    public int saveProduct(Product product){
        return productService.saveProduct(product);

    }
    @GetMapping("pssprodcut")
    public Map<String, Object> getproductdis(@RequestParam(required = false) Integer sid) {
        Map<String, Object> param = new HashMap<>();
        param.put("sid",sid);
           productService.getProductSid(param);
        return param;

    }
    /**
     * 下架商品查询
     *
     * @param page
     * @param pis
     * @return
     */
    @GetMapping("uppProductxia")
    public Map<String, Object> uppProductxia(Page  page ,@RequestParam(required = false) Integer pis) {
        Map<String, Object> param = new HashMap<>();
        if(!StringUtils.isEmpty(pis)){
            param.put("pis", pis);
        }

        IPage<Product> suAllPage = productService.productByProductpIsUppxia(page,param);

        List<Map<String, Object>> list = new ArrayList<>();
        suAllPage.getRecords().forEach(o -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("pid", o.getPId());
            map.put("pname", o.getPName());
            map.put("parea", o.getPArea());
            map.put("pml", o.getPMl());
            list.add(map);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "");
        result.put("count", suAllPage.getTotal());
        result.put("data", list);
        return result;
    }

    /**
     * 产品销售信息
     *
     * @param sid 产品类别
     * @return
     */
    @GetMapping("/prod-sales")
    public List<Map<String, Object>> prodSales(@RequestParam(required = false) Integer sid) {
        List<Map<String, Object>> list = productService.getProdSales(sid);
        return list;

    }
    //库存管理三级联动
    @GetMapping("/{pid}")
    public List<Product> findsortBysid(@PathVariable int pid){
        return productService.findproductvoAll(pid);
    }





}
